import * as THREE from "three";
import { Coin } from "./Coin";
import mitt from 'mitt';
import { Card } from "./Card";
import { SoundPlayer } from "./SoundPlayer";


export class Config {
  static CARDS_PER_ROW: number = 5;
  static CARDS_PER_COL: number = 7;
  static GAME_WIDTH: number = 800;
  static GAME_HEIGHT: number = 600;
  static CARD_WIDTH: number = 0;
  static CARD_HEIGHT: number = 0;
  static CARD_DEPTH: number = 0;

  static SCROLL_TIMER: number = 4800;
  static DROP_DOWN_TIMER: number = 800;
  static GAP_TIMER: number = 200;
  static CHOOSE_TIMER: number = 1500;

  static TOP_Y: number = 0;
  static BOTTOM_Y: number = 0;

  static MAP_URL: string = "./init.txt";
  static DATA_URL: string = "./init.txt";

  static MAP: Record<string, any> = {};

  static materialsMap: Record<any, any[]> = {};
  static meshMap: Record<any, THREE.Mesh> = {};
  static textureMap: Record<any, any> = {};
  static coin: Coin = null as any;
  static card: Card = null as any;

  static sounds:SoundPlayer = new SoundPlayer()

  static bus = mitt();

  static init(w: number, h: number): void {
    Config.GAME_WIDTH = w;
    Config.GAME_HEIGHT = h;
    Config.CARD_WIDTH = Config.GAME_WIDTH / Config.CARDS_PER_ROW;
    Config.CARD_HEIGHT = Config.GAME_HEIGHT / Config.CARDS_PER_COL;
    Config.CARD_DEPTH = Config.CARD_WIDTH * 0.12;

    console.log("Config.CARD_WIDTH", Config.CARD_WIDTH);
    console.log("Config.CARD_HEIGHT", Config.CARD_HEIGHT);

    const geometry = new THREE.BoxGeometry(Config.CARD_WIDTH, Config.CARD_HEIGHT, Config.CARD_DEPTH);

    const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 127, 128];
    nums.forEach((i) => {
      const textureLoader = new THREE.TextureLoader();
      const type = i > 100 ? 32 : i + 12;
      const texture = textureLoader.load(`./pic/handmah_${type}.png`);
      Config.textureMap[i] = texture;

      const materials = [
        new THREE.MeshPhongMaterial({ color: 0xcccccc }),  // x 负方向
        new THREE.MeshPhongMaterial({ color: 0xcccccc }),  // x 正方向
        new THREE.MeshPhongMaterial({ color: 0xcccccc }),  // y 负方向
        new THREE.MeshPhongMaterial({ color: 0xcccccc }),  // y 正方向
        new THREE.MeshBasicMaterial({ map: texture }),  // z 正方向
        new THREE.MeshPhongMaterial({ color: 0xeeeeee }),  // z 负方向
      ];

      Config.materialsMap[i] = materials;
      Config.meshMap[i] = new THREE.Mesh(geometry.clone(), materials);
    });
  }

  static getCoin() {
    return Config.coin.clone()
  }

  static getMesh(type: any): THREE.Mesh {
    return Config.meshMap[type].clone() as THREE.Mesh;
  }

  static getTexture(type: any): any{
    return Config.textureMap[type]
  }
}